!!M Inlines.f
!!S USER
	subroutine setdrug

  	implicit none
  	
!!SETC USINGINCLUDES = .FALSE.
	Include	'TYPES.F'
	Include	'DIALOGS.F'
	include 'OSUTILS.F'
	include 'SANE.F'

	Include	'Constants.inc'
	Include	'common.inc'
  	Include 'Drug.inc'
  	Include 'format.inc'
  	Include 'savestuff.inc'

	Extended	msgtime 

	pointer /ParamBlockRec/ MyCntrlParam

	record /decform/ f
	integer*1 r
	extended x
	string*255 s , ResourceNameString
	string*8 bagnumString

  	integer*4 itemHandle , prevPort
	pointer /DialogRecord/ ptr
	RECORD	/DRUGHandleType/ DRUGHandle

	pointer /resourcePtrType/ formatHandle
  	integer*2 ItemType , ItemHit , theID
	record /Rect/ box 

	external itemGlue , NumValFilter , itemGlue2

	integer*4	dose_handle  , volume_handle , conc_handle
	integer*4	dleft_handle , vleft_handle
	integer*2	bagnum , osErr

  	integer*2 new_drug_bag , Cancel
	integer*2 Drug_amount_num , Diluent_vol_num , Concentration_num
	integer*2 Drug_remaining_num , Diluent_remaining_num
	integer*2 default_num
	INTEGER*2 validateItem
  	parameter ( new_drug_bag 			=	1)
  	parameter ( Cancel					=	2)
  	parameter ( Drug_amount_num 		=	3)
  	parameter ( Diluent_vol_num			=	4)
  	parameter ( Concentration_num		=	5)
  	parameter ( Drug_remaining_num		=	6)
  	parameter ( Diluent_remaining_num 	=	7)
	parameter ( default_num				= 	9)
	PARAMETER ( validateItem 			=	10)
	
  	logical DONE
	
	extended temp_dose , temp_conc
	integer*4 temp_volume

	DATA bagnum		/ 0 /
	save bagnum

C  Set the SANE environment to round to the nearest value, and use fixed
C  notation with one significant figure for numeric to string conversion

	r = TONEAREST
	call SETROUND(r)
	f.style = FIXEDDECIMAL
	f.digits = 1
	
	ptr = GetNewDialog ( %val(int2(setdrug_DgId)) , %val(0) , %val(-1) )
	call GetPort ( prevPort )
	call SetPort ( %val(ptr) )

	formatHandle = GetResource ( %val ('Frm#') , %val(int2(setdrug_DgId)) )
	ptr^.window.refCon = formatHandle
	origField = .true.
	good = .true.

C  Set the proc for userItem enclosing all the validated editText fields

	call GetDItem ( %val(ptr) , %val(int2(validateItem))	, ItemType , itemHandle , box )
	ItemType = userItem+itemDisable
	call SetDItem ( %val(ptr) , %val(int2(validateItem)) , %val(ItemType) , 
	1				itemGlue , box )

C  Set the proc for the userItem enclosing the default button. This proc handles
C  dimming the button when there are invalid entries in the editText fields, and
C  draws the roundRect around the button

	call GetDItem ( %val(ptr) , %val(formatHandle^.Ptr^.okItem) , ItemType , 
	1				itemHandle , box )
	ItemType = userItem+itemDisable
	Call InsetRect ( box , %val(int2(-4)) , %val(int2(-4)) )
	call SetDItem ( %val(ptr) , %val(formatHandle^.Ptr^.defaultItem) , %val(ItemType) , 
	1				itemGlue2 , box )

	ptr^.aDefItem = formatHandle^.Ptr^.okItem

c  Get handles to dialog items

	call GetDItem ( %val(ptr) , %val( Drug_amount_num) , ItemType , dose_handle  , box )
	call GetDItem ( %val(ptr) , %val( Diluent_vol_num)		, ItemType , volume_handle, box )
	call GetDItem ( %val(ptr) , %val( Concentration_num)	, ItemType , conc_handle  , box )
	call GetDItem ( %val(ptr) , %val( Drug_remaining_num)	, ItemType , dleft_handle , box )
	call GetDItem ( %val(ptr) , %val( Diluent_remaining_num), ItemType , vleft_handle , box )

c  Set initial item values (Mike, I would take it as a personal favor if dose_hung could be
c  in units of mg instead of µg)

	temp_dose = dose_hung / 1000.
	call NUM2STR( f , temp_dose , %ref(s) )
	call SetIText ( %val(dose_handle) , %ref(s) )

	temp_volume = volume_hung
	call NumToString ( %val(temp_volume) , %ref(s) )
	call SetIText ( %val(volume_handle) , %ref(s) )

	temp_conc = drug_conc
	call NUM2STR( f , temp_conc , %ref(s) )
	call SetIText ( %val(conc_handle) , %ref(s) )

	dose_left = TheVolume * drug_conc
	call NUM2STR( f , dose_left / 1000. , %ref(s) )
	call SetIText ( %val(dleft_handle) , %ref(s) )

	fluid_left = TheVolume
	call NumToString ( %val(int4(TheVolume)) , %ref(s) )
	call SetIText ( %val(vleft_handle) , %ref(s) )

	call SelIText ( %val(ptr) , %val(Drug_amount_num) , %val(int2(0)) , %val(int2(32767)) )
	call ShowWindow ( %val(ptr) )
	
c  Wait for user to enter something

	DONE = .false.
	do while ( .not. DONE )
		call ModalDialog ( NumValFilter , ItemHit )
		If (ItemHit .eq. new_drug_bag ) then
			DONE = .true.

			drug_conc   = temp_conc
			volume_hung = temp_volume
			dose_hung   = temp_dose * 1000.0
			dose_left   = dose_hung

			bagnum = bagnum+1
			DRUGHandle.hndl = NewHandle ( %val(JSIZEOF(DRUGResourceType)) )
			call HLock ( %val(DRUGHandle.hndl) )
			DRUGHandle.hndl^.ptr^.bagnum = bagnum
			DRUGHandle.hndl^.ptr^.drug_type = SNPtype
			DRUGHandle.hndl^.ptr^.volume_hung = volume_hung
			DRUGHandle.hndl^.ptr^.dose_hung = temp_dose
			call TheTime ( %val (InDriver) , msgtime )
			DRUGHandle.hndl^.ptr^.bagTime = msgtime
			call NumToString ( %val(int4(bagnum)) , %ref(bagnumString) )
			ResourceNameString = 'New bag #' // bagnumString
			theID = UniqueID ( %val('DRUG') )
			call AddResource ( %val(DRUGHandle.hndl) , %val('DRUG') , %val(theID) ,
	1									%ref(ResourceNameString) )
			call UpdateResFile ( %val(curResFile()) )
			call ReleaseResource ( %val(DRUGHandle.hndl) )
				
			MyCntrlParam = NewPtr ( %val(JSIZEOF(ParamBlockRec)) )
			MyCntrlParam^.ioCompletion.pptr = 0
			MyCntrlParam^.csCode = SetVolume
			MyCntrlParam^.csParam(0) = volume_hung
			MyCntrlParam^.ioCRefNum = OutDriver
			osErr = PBStatus ( %val(MyCntrlParam) , %val(int2(TRUE)) )
	       
		ElseIf ( ItemHit .eq. Cancel ) then
			DONE = .true.

		ElseIf ( ItemHit .eq. Drug_amount_num ) then
			call GetIText ( %val(dose_handle) , %ref(s) )
			temp_dose = STR2NUM ( %ref(s) )
			temp_conc = temp_dose*1000.0 / temp_volume
			call NUM2STR( f , temp_conc , %ref(s) )
			call SetIText ( %val(conc_handle) , %ref(s) )
		ElseIf ( ItemHit .eq. Diluent_vol_num ) then
			call GetIText ( %val(volume_handle) , %ref(s) )
			call StringToNum ( %ref(s) , temp_volume )
			temp_conc = temp_dose*1000.0 / temp_volume
			call NUM2STR( f , temp_conc , %ref(s) )
			call SetIText ( %val(conc_handle) , %ref(s) )
		Else
			continue
		EndIf
	End Do

	call DisposDialog ( %val(ptr) )
	call ReleaseResource ( %val(formatHandle) )
	call SetPort ( %val(prevPort) )

	return
	end
